<?php
class MovieForm extends BaseForm {
  const FORM_NAME             = 'frm';
  
  const FIELD_ID              = 'id';
  const FIELD_TITLE           = 'title';
  const FIELD_YEAR            = 'year';
  const FIELD_RATING          = 'rating';
  const FIELD_SYNOPSIS        = 'synopsis';
  const FIELD_URL_POSTER      = 'url_poster';
  const FIELD_URL_IMDB        = 'url_imdb';
  const FIELD_URL_RT          = 'url_rt';
  const FIELD_URL_TRAILER     = 'url_trailer';

  private $movieId            = null;

  public function __construct($movieId = NULL) {
    $this->movieId = $movieId;
    parent::__construct();
  }

    public function configure() {
    
    $widgets = array(
            self::FIELD_ID            => new sfWidgetFormInputHidden(array(), array()),
            self::FIELD_TITLE         => new sfWidgetFormInput(array(), array('maxlength' => 255)),
            self::FIELD_YEAR          => new sfWidgetFormSelect(array('choices' => DatetimeUtil::getYearList() ), array('multiple' => false, 'expand' => true)),
            self::FIELD_RATING        => new sfWidgetFormInput(array(), array('maxlength' => 10)),   
            self::FIELD_SYNOPSIS      => new sfWidgetFormTextarea(),
            self::FIELD_URL_POSTER    => new sfWidgetFormInputFile(),
            self::FIELD_URL_IMDB      => new sfWidgetFormInput(array(), array('maxlength' => 255)),
            self::FIELD_URL_RT        => new sfWidgetFormInput(array(), array('maxlength' => 255)),
            self::FIELD_URL_TRAILER   => new sfWidgetFormInput(array(), array('maxlength' => 255))
    );

    $labels = array(
            self::FIELD_TITLE             => 'Movie Title<req />',
            self::FIELD_YEAR              => 'Year',
            self::FIELD_RATING            => 'Rating',
            self::FIELD_SYNOPSIS          => 'Synopsis',
            self::FIELD_URL_POSTER        => 'Poster',
            self::FIELD_URL_IMDB          => 'IMDB URL',
            self::FIELD_URL_RT            => 'RT URL',
            self::FIELD_URL_TRAILER       => 'Trailer URL'
    );

    $this->setWidgets($widgets);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_TITLE        => new sfValidatorString(array(), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_TITLE]))))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) { 
    return $values;
  }

  public function populateData(MOVIE $movie) {
    $this->setDefault(self::FIELD_ID, $movie->getId());
    $this->setDefault(self::FIELD_TITLE, $movie->getTitle());
    $this->setDefault(self::FIELD_YEAR, $movie->getYear());
    $this->setDefault(self::FIELD_RATING, $movie->getRating());
    $this->setDefault(self::FIELD_SYNOPSIS, $movie->getSynopsis());
    $this->setDefault(self::FIELD_URL_POSTER, $movie->getUrlPoster());
    $this->setDefault(self::FIELD_URL_IMDB, $movie->getUrlImdb());
    $this->setDefault(self::FIELD_URL_RT, $movie->getUrlRt());
    $this->setDefault(self::FIELD_URL_TRAILER, $movie->getUrlTrailer());
  }
  
}

?>